package com.gzw.dms.activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.bean.Constant;
import com.gzw.dms.bean.DriverInfo;
import com.gzw.dms.bean.OrderNumberInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SplashActivity extends AppCompatActivity {
    private LocationClient mLocationClient;
    private Context mContext;
    long startTime1;
    long startTime2;
    int time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        setContentView(R.layout.activity_splash);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mLocationClient = ((MyApplication) getApplication()).mLocationClient;
        new MyHandler().sendEmptyMessageDelayed(0, 2000);
        time = 5;
        initLocation();
//        SignShipOrder signShipOrder = new SignShipOrder();
//        signShipOrder.execute();
    }

    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initData();
            mLocationClient.start();

        }
    }

    public void initData() {

        SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                Context.MODE_PRIVATE);

        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(sharedPreferences.getString("passWord", ""));
        userInfo.setUsername(sharedPreferences.getString(Constant.USER_NAME_KEY, ""));
        userInfo.setId(sharedPreferences.getString(Constant.USER_ID_KEY, ""));
        userInfo.setDriver_id(sharedPreferences.getString(Constant.DRIVER_ID_KEY, ""));
        userInfo.setSuppliers_id(sharedPreferences.getString(Constant.SUPPLIERS_ID_KEY, ""));
        userInfo.setFlag(sharedPreferences.getString(Constant.FLAG_KEY, ""));
        userInfo.setRealName(sharedPreferences.getString(Constant.NAME_KEY, ""));
        userInfo.setUserPhone(sharedPreferences.getString("userPhone", ""));
        userInfo.setCustomer_name(sharedPreferences.getString("customerName", ""));
        userInfo.setDid(sharedPreferences.getString("did", ""));

        //本地读取list
        String listStr = sharedPreferences.getString("role", "");
        String userif = sharedPreferences.getString("userinfo", "");
        Gson gson = new Gson();
        List<String> list = gson.fromJson(listStr, new TypeToken<List<String>>() {
        }.getType());
        UserInfo userInfo2 = JsonUtil.getPerson(userif, UserInfo.class);
        userInfo.setUserAppRole(list);
        MyApplication.setIsUserLogin(sharedPreferences.getBoolean(Constant.IS_USER_LOGIN_KEY, false));
        MyApplication.setFlag(1);
        MyApplication.setUserInfo(userInfo);

        Intent mIntent = new Intent();
        //1.司机用户 2.供应商用户
        if (MyApplication.getIsUserLogin() && MyApplication.getUserInfo().getUserAppRole() != null) {
            Log.d("debug", "initData: " + MyApplication.getIsUserLogin());
            if (MyApplication.getIsUserLogin()) {
                if (MyApplication.getUserInfo().getFlag().equals("1")) {
                    mIntent.setClass(this, Home1Activity.class);
                    startActivity(mIntent);

                } else if (MyApplication.getUserInfo().getFlag().equals("2")) {
                    mIntent.setClass(this, Home1Activity.class);
                    startActivity(mIntent);

                } else if (MyApplication.getUserInfo().getFlag().equals("0")) {
                    mIntent.setClass(this, Home1Activity.class);
                    startActivity(mIntent);

                } else if (MyApplication.getUserInfo().getFlag().equals("3")) {
                    mIntent.setClass(this, Home1Activity.class);
                    startActivity(mIntent);

                } else if (MyApplication.getUserInfo().getFlag().equals("4")) {
                    mIntent.setClass(this, Home1Activity.class);
                    startActivity(mIntent);

                } else {
                    mIntent.setClass(this, LoginActivity.class);
                    startActivity(mIntent);
                }
                finish();
            } else {
                mIntent.setClass(this, LoginActivity.class);
                startActivity(mIntent);
                finish();
            }
        }else {
            mIntent.setClass(this, LoginActivity.class);
            startActivity(mIntent);
            finish();
        }
    }
    public class SignShipOrder extends AsyncTask<DriverInfo, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(DriverInfo... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetLocationTime"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (result.trim() == null) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                    initLocation();
                    mLocationClient.start();
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    if (successMsg.trim() == null || successMsg.trim().equals("")){

                    }else {
                        time = Integer.parseInt(successMsg);
                    }


                }
            }
        }

        @Override
        protected void onCancelled() {

        }
    }


    //设置定位选项
    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 1000 * time;
        option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认false，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);

    }


}